layui.use(['table','laydate','layer',"form"],function(){
       var layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        table = layui.table;


    function dateChange(num = 0,date = false) {
        if (!date) {
            date = new Date();//没有传入值时,默认是当前日期
            date = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
        }
        date += " 00:00:00";//设置为当天凌晨12点
        date = Date.parse(new Date(date))/1000;//转换为时间戳
        date += (86400) * num;//修改后的时间戳
        var newDate = new Date(parseInt(date) * 1000);//转换为时间
        return newDate.getFullYear() + '-' + (newDate.getMonth() + 1) + '-' + newDate.getDate();
    }

    laydate.render({
        elem: '#startDate',
        format: "yyyy-MM-dd",
        value: dateChange(-15),
    });
    laydate.render({
        elem: '#endDate',
        format: "yyyy-MM-dd",
        value: new Date(),
    });

    var  tableIns = table.render({
        elem: '#daySale',
        even: true,
        sort:true,
        url:ctx+"/sale/countSaleByDay",
        cellMinWidth : 95,
        height : "auto",
        toolbar: "#toolbarDemo",
        totalRow: true,
        id : "daySaleTable",
        cols : [[
            {field: "date", title:'销售日期',align:'center',totalRowText: '合计（￥）'},
            {field: 'amountCost', title: '成本金额(￥)', minWidth:80, align:"center",totalRow: true},
            {field: 'amountSale', title: '销售金额（￥）', minWidth:80, align:'center',totalRow: true},
            {field: 'amountProfit', title: '盈利金额(￥)', align:'center',minWidth:80,totalRow: true,sort: true}
        ]]
    });

    // 多条件搜索
    $(".search_btn").on("click",function(){
        var  startDate= $("input[name='startDate']").val();
        var endDate= $("input[name='endDate']").val();
        table.reload("daySaleTable",{
            where: {
                begin:startDate,
                end:endDate
            }
        })
    });

});
